<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex'
	import {
		TokenKey,
		qiyuKey
	} from '@/config/index.js'
	import { check_vote_password } from "@/api/bladeMonarch/index.js"
	
	// #ifdef APP-PLUS
	// var qiyuModule = uni.requireNativePlugin("Netease-QiyuModule")
	import APPUpdate, {
		getCurrentNo
	} from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
	// #endif
	export default {
		onLaunch: function(e) {
			// uni.hideTabBar()
			e.query.content && uni.setStorageSync("invite_code",e.query.content);
			if(e.query.inviter_user_id) {
				uni.setStorageSync("inviter_user_id",e.query.inviter_user_id)
			} 
			// 七鱼客服初始化
			// #ifdef H5
				(function(w, d, n, a, j) {
					w[n] = w[n] || function() {      
						return (w[n].a = w[n].a || []).push(arguments)
					};
					j = d.createElement('script');
					j.async = true;
					j.src = `https://qiyukf.com/script/${qiyuKey}.js?hidden=1`;
					d.body.appendChild(j);
				})(window, document, 'ysf');

				// 预加载图片
				this.preLoad();
			
				function hello(name) {
				      console.log('Hello, ' + name + '!');
					  alert(name)
				      return 'Hello, ' + name + '!';
				}
				
				
				
				let info=uni.getSystemInfoSync();
				let overwrite=()=>{   
					let appStatusHeight=window.localStorage.getItem('statusHeight');
					// alert(appStatusHeight)
					info.statusBarHeight=appStatusHeight? Number(appStatusHeight):info.statusBarHeight
					let html=document.getElementsByTagName('html')[0]
					html.style.setProperty('--status-bar-height',info.statusBarHeight+'px')
					return info;
				}
				
				uni.getSystemInfoSync=overwrite;
				// window.Android={
				// 	setToken(token){
				// 		alert(token)
				// 		return token
				// 	}	
				// }
					// e.query.content
				let {token}=e.query;
				if(token){
					uni.setStorageSync('token',token);
				}
			// #endif
			
			
			// #ifdef APP-PLUS
				try{
					APPUpdate();
					console.log("----------------自动更新启动---------------")
				}catch{
					console.log("----------------自动更新错误---------------")
				}
				try{
					
					qiyuModule.initSdk({
						appKey:qiyuKey,
						appName:"lvBox"
					})
					console.log("----------------七鱼初始化成功---------------")
				}catch(e){
					console.log("----------------七鱼初始化失败---------------")
				}
			// #endif
			this.getConfig().then(()=>{
				// 是否跳转开屏页
				// if (uni.getStorageSync('isOpenCreen') == 1 && !e.query.content && !e.query.id) {
				// 	uni.redirectTo({
				// 		url: "/pages/openCreen/openCreen"
				// 	})
				// }
			})
			// let pageConfig=this.getPageConfig();
			// Promise.all([config,pageConfig])
			this.appInit();
			this.routerInterceptor();
			let _this = this;
			uni.addInterceptor('navigateTo', {
				success() {
					_this.routerInterceptor()
				}
			})
			uni.addInterceptor('redirecTo', {
				success() {
					_this.routerInterceptor()
				}
			})
			uni.addInterceptor('switchTab', {
				success() {
					_this.routerInterceptor()
				}
			})
			uni.addInterceptor('navigateBack', {
				success(res) {
					_this.routerInterceptor()
				}
			})
			
			// 设置语言包
			this.switchLang('cn')
			
		
			// 隐藏原生tabbar
			// uni.hideTabBar()
		},
		onShow() {
			
			//  #ifdef APP-PLUS
				setTimeout(()=>{
					if (!plus.runtime.arguments) return;
					let appOpenParam = plus.runtime.arguments
					plus.runtime.arguments = null;
					plus.runtime.arguments = "";
					appOpenParam = appOpenParam.replace("lvBoxnft://", '')
					let arr = appOpenParam.split('=')
					if (arr[0] == 'toPage') {
						let urlString = decodeURIComponent(arr[1])
						this.$u.route(urlString)
					}
				},300)
				
			// #endif
			this.analysisoftheadhesiveboard()
			// this.check_vote_password_change('34324')
		},
		computed: {
			...mapState('user', ['userInfo'])
		},
		methods: {
			...mapActions('user', ['getUserInfo']),
			...mapActions('config', ['getConfig','appInit','getPageConfig']),
			...mapActions('push',['initPush']),
			...mapMutations('lang',['switchLang']),
			routerInterceptor() {
				let token = uni.getStorageSync(TokenKey)
				if (token && (!this.userInfo.id)) {
					this.$nextTick(() => {
						this.getUserInfo()
					})
				}
			},
			//预加载图片
			preLoad() {
				let img = [
					`${this.$ossUrl}/static/gif/hc.gif`,
					`${this.$ossUrl}/static/gif/mh.gif`,
					'/static/bg/cj-page-bg.png'
				]
				let count = 0;
				img.forEach(item => {
					let image = new Image();
					image.src = item;
					console.log(item)
					image.onload = () => { //加载完毕事件
						count++
						// 计算图片加载的百分数，绑定到percent变量
						let percentNum = Math.floor(count / img.length * 100)
						let percent = `${percentNum}%`
						console.log(percent)
					}
				})
			},
			/**
			 * 解析粘贴板
			 */
			analysisoftheadhesiveboard(){
				console.log('进来了');
				let _this = this
				uni.getClipboardData({
					success: function (res) {
						console.log(res.data,'成功');
						_this.check_vote_password_change(res.data)
					},
					fail: function (res) {
						console.log(res,'失败');
					},
					complete: function(){
						console.log('3333');
					}
				});
			},
			async check_vote_password_change(vote_token = ''){
				console.log('请求');
				if(!uni.getStorageSync('token')) return
				if(uni.getStorageSync('vote_token') === vote_token) return
				const { code,data } = await check_vote_password({vote_token})
				console.log(data);
				if(code != 1) return
				uni.setStorageSync('vote_token',vote_token)
				setTimeout(() => {
					uni.navigateTo({
						url:'/pages/bladeMonarch/index?id=' + data
					})
				},1000)
			}
		}
	}
</script>

<style lang="scss">
	@import 'static/css/theme.scss';
	@import "uview-ui/index.scss";
	.page-bg {
		// background-image: url($IMG_URL + "/static/bg/page-bg.png");
		background-size: 750rpx auto;
		background-repeat: no-repeat;
		background-color: $main-bg-color;

		>#my-nav /deep/ .u-navbar--fixed {
			// background-image: url($IMG_URL + '/static/bg/page-bg.png');
			background-position: top;
			background-size: 100% auto;
			background-color: $main-bg-color;
		}

		.diy-nav {
			// background-image: url($IMG_URL + '/static/bg/page-bg.png');
			background-position: top;
			background-size: 100% auto;
			background-color: $main-bg-color;
		}
	}

	page {
		-webkit-overflow-scrolling: touch; // ios滑动不流畅
		height: 100%;
		width: 100%;
		font-size: 30rpx;
		word-break: break-all; //英文文本不换行
		white-space: normal;
		background-color: $main-bg-color;
		color: #000;
	}

	view,
	text,
	input {
		box-sizing: border-box;
	}
	
	

	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
		display: none;
	}
	
	// 解决回填后带背景色问题
	:-webkit-autofill,
	:-webkit-autofill:hover,
	:-webkit-autofill:focus,
	:-webkit-autofill:active {
		-webkit-transition-delay: 99999s;
		-webkit-transition: color 99999s ease-out,
		  background-color 99999s ease-out;
	}
	.no_text {
		white-space: normal;
	}
	
	.pt-box {
		width: 84rpx;
		height: 34rpx;
		background: #A263F3;
		border-radius: 8rpx;
		font-size: 22rpx;
		color: #fff;
		text-align: center;
		line-height: 34rpx;
		margin-left: 10rpx;
	}

	// /deep/ .u-popup .u-fade-enter-active {
	// 	background-color: rgba(0, 0, 0, 0.7) !important;
	// }
</style>
